package com.nbstar.film.spider.engine;

import com.nbstar.film.spider.domain.FilmSpiderDomain;
import com.nbstar.film.spider.excel.ShaoErExcelWriter;
import com.nbstar.film.spider.excel.SiChuanExcelReader;
import com.nbstar.film.spider.excel.SiChuanExcelWriter;
import com.nbstar.film.spider.excel.YunNanExcelReader;
import com.nbstar.film.spider.pojo.FilmExcelInfo;
import com.nbstar.film.spider.pojo.FilmInfo;
import com.nbstar.film.spider.spider.*;
import com.nbstar.film.spider.thread.ProxyThread;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ShaoErEngine extends BaseSpiderEngine {
    @Override
    public void executeDomain(FilmSpiderDomain filmSpiderDomain) throws Exception {
        FilmSpider youkuSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_YOUKU);
        FilmInfo youkuFilmInfo = null;


        youkuFilmInfo = ((BaseSpider) youkuSpider).run(filmSpiderDomain.getRaw().getFilmName());
        log.debug(youkuFilmInfo.toString());

        FilmSpider iqySpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_IQY);;
        FilmInfo iqyFilmInfo = ((BaseSpider) iqySpider).run(filmSpiderDomain.getRaw().getFilmName());
        log.debug(iqyFilmInfo.toString());

        FilmSpider qqSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_QQ);;
        FilmInfo qqFilmInfo = ((BaseSpider) qqSpider).run(filmSpiderDomain.getRaw().getFilmName());
        log.debug(qqFilmInfo.toString());

        filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_YOUKU, youkuFilmInfo);
        //下面开始写excel
        filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_IQY, iqyFilmInfo);
        filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_QQ, qqFilmInfo);

    }

    @Override
    public boolean checkDomainSucc(FilmSpiderDomain domain) {
        FilmInfo youkuFilmInfo =  domain.get(FilmSpiderDomain.SPIDER_FROM_YOUKU);
        FilmInfo iqyFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_IQY);
        FilmInfo qqFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_QQ);
        if (youkuFilmInfo != null &&
                iqyFilmInfo != null &&
                qqFilmInfo != null) {
            return youkuFilmInfo.isSuccess() && iqyFilmInfo.isSuccess() && qqFilmInfo.isSuccess();
        }

        return  false;
    }

    @Override
    public void initEngine() {
        registSpider(FilmSpiderDomain.SPIDER_FROM_YOUKU, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_YOUKU));
        registSpider(FilmSpiderDomain.SPIDER_FROM_IQY, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_IQY));
        registSpider(FilmSpiderDomain.SPIDER_FROM_QQ, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_QQ));
        //获取代理
//        this.excelReader = new SiChuanExcelReader();
        this.excelReader = new YunNanExcelReader();
        this.excelWriter = new ShaoErExcelWriter(resultSheetName, resultExcelName);
        ProxyThread proxyThread = new ProxyThread();
        proxyThread.start();
        excelReader.setFilmSpiderQueue(filmSpiderQueue);
    }
}
